﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PhoneDBModel;

public partial class Admin_Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["IsAdmin"] == null)
        {
            Response.Redirect("~/Login.aspx");
        }
        if (Page.IsPostBack == false)
        {
            TextBox1.Text = DateTime.Now.ToShortDateString();
            AfiseazaApeluri(DateTime.Now);
        }
        
    }
    protected void btn_Click(object sender, EventArgs e)
    {
        DateTime dt = DateTime.Parse(TextBox1.Text);
        AfiseazaApeluri(dt);
    }

    private void AfiseazaApeluri(DateTime dt)
    {
        litData.Text = dt.ToShortDateString();
        PhoneDBEntities dbe = new PhoneDBEntities();
        var list = dbe.CallLogs.Where(x => x.Timestamp.Day == dt.Day 
            && x.Timestamp.Year == dt.Year && x.Timestamp.Month == dt.Month)
            .OrderByDescending(x => x.Timestamp);

        foreach (CallLog cl in list)
        {
            cl.PhoneCalled = CheckPhoneNumber.GetCountryFromCode(CheckPhoneNumber.GetCountry(cl.PhoneCalled));
        }
        gvLastCalls.DataSource = list;
        gvLastCalls.DataBind();
    }
}